import collections
class Solution(object):
    def killProcess(self, pid, ppid, kill):
        """
        :type pid: List[int]
        :type ppid: List[int]
        :type kill: int
        :rtype: List[int]
        """
        n = len(pid)
        d = collections.defaultdict(set)
        for i in range(n):
            if ppid[i] != 0:
                d[ppid[i]].add(pid[i])
        kill_set = set()
        now_set = {kill}
        while now_set:
            kill_set.update(now_set)
            next_set = set()
            for item in now_set:
                for child in d[item]:
                    next_set.add(child)
            now_set = next_set
        return list(kill_set)




data = Solution()
pid = [1, 3, 10, 5]
ppid = [3, 0, 5, 3]
kill = 5
print(data.killProcess(pid, ppid,kill))
